<?php
declare (strict_types=1);

namespace app\admin\controller\advertiser;

use app\admin\model\base\UserSubscribeLog;
use app\admin\service\advertiser\AdvertiserTaskService;
use app\BaseController;
use think\facade\Request;
use think\response\Json;
use think\facade\Db;

/**
 * 广告主推广任务(公众号和小程序)
 */
class AdvertiserTask extends BaseController
{

    /**
     * 广告主推广任务待审核列表
     * @param AdvertiserTaskService $advertiserTaskService
     * @return Json
     */
    public function reviewList(AdvertiserTaskService $advertiserTaskService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['task_name'] = Request::param('task_name/s');
        $params['ad_user_id'] = Request::param('ad_user_id/d');
        $params['type'] = Request::param('type/d', 0);
        $params['start_time'] = Request::param('start_time/d', 0);
        $params['end_time'] = Request::param('end_time/d', 0);
        $params['status'] = 2;
        return $advertiserTaskService->taskList($params);
    }

    /**
     * 广告主推广任务审核
     * @param AdvertiserTaskService $advertiserTaskService
     * @return Json
     */
    public function taskReview(AdvertiserTaskService $advertiserTaskService): Json
    {
        $params['task_id'] = Request::param('task_id/d', 0);
        $params['status'] = Request::param('status/d', 2);
        return $advertiserTaskService->taskReview($params);
    }

    /**
     * 广告主推广任务列表
     * @param AdvertiserTaskService $advertiserTaskService
     * @return Json
     */
    public function taskList(AdvertiserTaskService $advertiserTaskService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['task_name'] = Request::param('task_name/s');
        $params['ad_user_id'] = Request::param('ad_user_id/d');
        $params['status'] = Request::param('status/d', 0);
        $params['type'] = Request::param('type/d', 0);
        $params['start_time'] = Request::param('start_time/d', 0);
        $params['end_time'] = Request::param('end_time/d', 0);
        return $advertiserTaskService->taskList($params);
    }

    /**
     * 广告主推广任务上下架
     * @param AdvertiserTaskService $advertiserTaskService
     * @return Json
     */
    public function taskEnable(AdvertiserTaskService $advertiserTaskService): Json
    {
        $params['task_id'] = Request::param('id/d', 0);
        $params['enable'] = Request::param('enable/d', 0);
        return $advertiserTaskService->taskEnable($params['task_id'], $params['enable']);
    }

    /**
     * 广告主推广任务排序列表
     * @param AdvertiserTaskService $advertiserTaskService
     * @return Json
     */
    public function sortList(AdvertiserTaskService $advertiserTaskService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['status'] = Request::param('status/d', 0);
        $params['type'] = Request::param('type/d');
        return $advertiserTaskService->sortList($params);
    }

    /**
     * 广告主推广任务设置排序值
     * @param AdvertiserTaskService $advertiserTaskService
     * @return Json
     */
    public function taskSort(AdvertiserTaskService $advertiserTaskService): Json
    {
        $task_id = Request::param('task_id/d', 0);
        $orderby = Request::param('orderby/d', 0);
        return $advertiserTaskService->taskSort($task_id, $orderby);
    }

    /**
     * 广告主推广任务明细记录
     * @param AdvertiserTaskService $advertiserTaskService
     * @return Json
     */
    public function taskLog(AdvertiserTaskService $advertiserTaskService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['task_name'] = Request::param('task_name/s', '');
        $params['unionid'] = Request::param('unionid/s', '');
        $params['start_time'] = Request::param('start_time/d', 0);
        $params['end_time'] = Request::param('end_time/d', 0);
        return $advertiserTaskService->taskLog($params);
    }

    /**
     * 推广任务关注情况统计
     * @return Json
     */
    public function taskSubscribeStatistics(): Json
    {
        $app_id = Request::param('app_id/s', 0);
        $page = Request::param('page/d', 1);
        $limit = Request::param('limit/d', 10);
        //生成对应的日期
        $data = [];
        for ($i = 0; $i < $limit; $i++) {
            $t = time() - ($i * 86400) - (($page - 1) * $limit * 86400);
            $data[$i]['date'] = date('Y-m-d', $t);
            //查询数据库
            $data[$i]['count_subscribe'] = UserSubscribeLog::where(['appid'=>$app_id,'subscribe'=>1])->whereDay('create_time', $data[$i]['date'])->count();
            $data[$i]['count_unsubscribe'] = UserSubscribeLog::where(['appid'=>$app_id,'subscribe'=>0])->whereDay('create_time', $data[$i]['date'])->count();
        }
        //返回参数
        $return = [
            'total' => 365,
            'per_page' => $limit,
            'current_page' => $page,
            'last_page' => intval(ceil(365 / $limit)),
            'data' => $data,
        ];
        return jsonReturn(1, $return);
    }


}
